package com.lxblyw.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lxblyw.entity.Idea;
import com.lxblyw.util.DBHelp;
import com.lxblyw.util.RowMapper;

public class IdeaDao {
	private DBHelp<Idea> dbhelp = new DBHelp<Idea>();
	
	private class IdeaRowMapper implements RowMapper<Idea> {

		public Idea mapperRow(ResultSet rs) throws SQLException {
			Idea idea = new Idea();
			idea.setId(rs.getString("id"));
			idea.setTitle(rs.getString("title"));
			idea.setContent(rs.getString("content"));
			idea.setCreatetime(rs.getString("createtime"));
			idea.setEmpolyeeid(rs.getString("employeeid"));
			idea.setProjectid(rs.getString("projectid"));
			return idea;
		}
	}
	public List<Idea> findByProjectId(String projectid) {
		String sql = "select * from t_idea where projectid = ? order by createtime desc";
		return dbhelp.executeQueryForList(sql,new IdeaRowMapper(),projectid);
	}
	public void save(Idea idea) {
		String sql = "insert into t_idea(id,title,content,createtime,employeeid,projectid) values(?,?,?,?,?,?)";
		dbhelp.executeSQL(sql, idea.getId(),idea.getTitle(),idea.getContent(),idea.getCreatetime(),idea.getEmployeeid(),idea.getProjectid());
	}
	public void update(String title, String content, String id) {
		String sql = "update t_idea set title = ?,content = ? where id = ?";
		dbhelp.executeSQL(sql, title,content,id);
	}
	public void delete(String id) {
		String sql1 = "delete from t_ideacomment where t_ideaid = ?";
		String sql2 = "delete from t_idea where id = ?";
		dbhelp.executeSQL(sql1, id);
		dbhelp.executeSQL(sql2, id);
	}
	public Idea findById(String id) {
		String sql = "select * from t_idea where id = ?";
		return dbhelp.executeQueryForObject(sql, new IdeaRowMapper(), id);
	}
	
}
